package exportExcel;

import jxl.*;
import jxl.write.*;
import jxl.write.biff.*;

public class JExcelUtils{
    public JExcelUtils() {
    }

    public void copyRows(WritableSheet sourceSheet, WritableSheet targetSheet, int pStartRow, int pEndRow, int pPosition) throws
            RowsExceededException, WriteException {
        if(pStartRow<0 || pEndRow<0){
            return;
        }

        //Merge
        Range region=null;
        Range[] bigMergedRegion = sourceSheet.getMergedCells();
        for(int i=0; i < bigMergedRegion.length;i ++){
            region = bigMergedRegion[i];
            if(region.getTopLeft().getRow() >= pStartRow &&
               region.getBottomRight().getRow() <= pEndRow){
               targetSheet.mergeCells(region.getTopLeft().getColumn(),
               region.getTopLeft().getRow()-pStartRow+pPosition,
               region.getBottomRight().getColumn(),
               region.getBottomRight().getRow()-pStartRow+pPosition); //merge tu cot, tu dong, den cot, den dong
            }
        }

        //Copy cells' content
        Cell[] rows = null;
        Cell cell = null;
        CellView width = null;
        CellView heigth = null;
        for (int i = pStartRow; i <= pEndRow; i++) {
            rows = sourceSheet.getRow(i);
            heigth = sourceSheet.getRowView(i);
            targetSheet.setRowView(i-pStartRow+pPosition, heigth); //set rows's height
            for (int j = 0; j < rows.length; j++) {
                cell = rows[j];
                width = sourceSheet.getColumnView(j);
                targetSheet.setColumnView(j, width); //set columns's width
                CellType ct = cell.getType();
                ct.toString();
                //copy cell
                WritableCell wc = ( (WritableCell) cell).copyTo(j,
                                                         i-pStartRow+pPosition);
                targetSheet.addCell(wc);
            }
        }
    }

    /***
     * Setting cho sheet thuc thi tu 1 sheet template
     */
    public void setSheetSettingsFromTemplate(WritableSheet sourceSheet, WritableSheet targetSheet){
        targetSheet.getSettings().setPaperSize(sourceSheet.getSettings().getPaperSize());
        targetSheet.getSettings().setOrientation(sourceSheet.getSettings().getOrientation());
        targetSheet.getSettings().setHeaderMargin(sourceSheet.getSettings().getHeaderMargin());
        targetSheet.getSettings().setFooterMargin(sourceSheet.getSettings().getFooterMargin());
        targetSheet.getSettings().setTopMargin(sourceSheet.getSettings().getTopMargin());
        targetSheet.getSettings().setBottomMargin(sourceSheet.getSettings().getBottomMargin());
        targetSheet.getSettings().setLeftMargin(sourceSheet.getSettings().getLeftMargin());
        targetSheet.getSettings().setRightMargin(sourceSheet.getSettings().getRightMargin());
        targetSheet.getSettings().setHeader(sourceSheet.getSettings().getHeader());
        targetSheet.getSettings().setFooter(sourceSheet.getSettings().getFooter());
    }
}
